package offer;

/**
 * @Author SSK
 * @Date Created in 19:29 2019/8/4
 * @Description 把字符串转换成整数
 * @Version 1.0
 */
public class T50 {
    public int StrToInt(String str) {
        if (str==null||str.length()==0) {
            return 0;
        }
        int ans = 0;
        for (int i = 0; i < str.length(); i++) {
             char ch = str.charAt(i);
             if (i==0&&(ch=='-'||ch=='+')) {
                 continue;
             }
             if (ch>='0'&&ch<='9') {
                 ans*=10;
                 ans+=ch-'0';
             }else {
                 return 0;
             }
        }
        if (str.charAt(0)=='-') {
            ans*=-1;
        }
        return ans;
    }
}
